/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.autocheck.impl;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundTktDto;
import com.ceair.flight.refund.dto.appmoblie.RefundSegDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 *
 * @author youguilin
 */
@Service("basicAutoChecker")
@Order(value = 10)
public class BasicAutoChecker extends AbstractAutoChecker {

    private static final Logger LOG = LoggerFactory.getLogger(BasicAutoChecker.class);

    public BasicAutoChecker() {
        super("basicAutoChecker");
    }

    @Override
    public boolean autoCheck(RefundApplyDto refundApply) {
        if (isFeeGreaterThanFacePrice(refundApply)) {
            LOG.info("Refund fee is greater than ticket price, will stop auto first audit");
            return false;
        }
        if (isPartialUsedRefund(refundApply)) {
            LOG.info("Partial used refund ticket, will stop auto first audit");
            return false;
        }
        if (this.isPromoteOrder(refundApply)) {
            LOG.info("Promotion order, will stop auto first audit");
            return false;
        }
        if (this.isRescheduleTicket(refundApply)) {
            LOG.info("Ticket has been rescheduled, will stop auto first audit");
            return false;
        }
        if (this.isPointTicket(refundApply)) {
            LOG.info("Ticket buy with points, will stop auto first audit");
            return false;
        }
        return super.goNextOrReturn(refundApply);

    }

    /**
     * *
     * 改期票默认不进行自动审核
     *
     * @return
     */
    private boolean isRescheduleTicket(final RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        return refundTkt.isReschedule();
    }

    /**
     * 当退票手续费大于票面价时, 停止自动一审
     *
     * @param refundApply
     * @return
     */
    private boolean isFeeGreaterThanFacePrice(final RefundApplyDto refundApply) {
        BigDecimal refundFee = refundApply.getRefundFee();
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        BigDecimal ticketPrice = refundTkt.getTktPrice();
        return refundFee != null && refundFee.compareTo(BigDecimal.ZERO) > 0
                && ticketPrice.compareTo(BigDecimal.ZERO) > 0 && refundFee.compareTo(ticketPrice) > 0;

    }

    /**
     * 部分使用的票, 停止自动一审
     *
     * @param refundApply
     * @return
     */
    private boolean isPartialUsedRefund(final RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        Set<RefundSegDto> refundSegs = refundTkt.getRefundSegs();
        List<RefundSegDto> segments = new ArrayList<>(refundSegs);
        Collections.sort(segments);
        int indexNo = segments.get(0).getIndexNo();
        return indexNo > 1;
    }

    /**
     * 促销订单, 停止自动一审
     *
     * @param refundApply
     * @return
     */
    private boolean isPromoteOrder(final RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        BigDecimal promoteAmt = refundTkt.getPromoteAmt();
        LOG.info("Order promote amount value {}", promoteAmt);
        return promoteAmt != null;
    }

    private boolean isPointTicket(final RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        Long points = refundTkt.getTktPoints();
        LOG.info("Order points {}", points);
        return points != null && points > 0;
    }
}
